/** HistoryPanel */
package client.ui.panel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import client.service.AppointmentService;
import domain.appointment.Appointment;
import domain.patient.Patient;

/**
 * @author jessienewman
 * @version $Revision: 1.0 $
 */
public class HistoryPanel extends DefaultPanel{
	/**
	 * Field patient.
	 */
	private Patient patient;

	/**
	 * Field app.
	 */
	private AppointmentPanel app;
	
	/**
	 * Field c.
	 */
	private final GridBagConstraints c;
	
	/**
	 * Field center.
	 */
	private final JPanel center;
	
	/**
	 * Field STARTYEAR.
	 */
	private static final Integer STARTYEAR = 1900;
	
	/**
	 * Field WIDTH.
	 */
	private static final Integer WIDTH = 500;
	
	/**
	 * Field HEIGHT.
	 */
	private static final Integer HEIGHT = 300;
	
	/**
	 * Field FILLERWIDTH.
	 */
	private static final Integer FILLERWIDTH = 100;
	
	/**
	 * Field FILLERHEIGHT.
	 */
	private static final Integer FILLERHEIGHT = 50;

	/**
	 * Constructor for HistoryPanel.
	 */
	public HistoryPanel() {
		c = new GridBagConstraints();
		center = new JPanel();
		center.setLayout(new GridBagLayout());
		center.setBackground(Color.WHITE);
		this.add(center, BorderLayout.CENTER);
	}

	/**
	 * Method addPatient.
	 * @param patient Patient
	 */
	public void addPatient(Patient patient) {
		center.removeAll();
		this.patient = patient;
		app = new AppointmentPanel();
		final List<Appointment> appointments = 
			AppointmentService.getPatientAppointments(patient.getUserId());
		//ClientManager.SESSION.getUserId();
		app.addAppointments(appointments, patient);
		//app.setNewAppointmentViewable(false);

		c.gridx = 0;
		c.gridy = 0;
		center.add(new JLabel("Appointments:"));
		c.gridy++;
		c.gridwidth = 2; // $codepro.audit.disable numericLiterals
		center.add(app.getCenterPanel(), c);

		if (appointments.size() == 0) {
			return;
		}

		final XYSeries series = new XYSeries("XY Graph");
		for (Appointment a : appointments) {
			series.add(a.getDate().getYear() + STARTYEAR, 
					a.getTreatmentRecord().getWeight());
		}
		final XYSeriesCollection data = new XYSeriesCollection(series);
		final JFreeChart chart = ChartFactory.createXYLineChart(
				"Weight over Time", "Year", "Weight", data, 
				PlotOrientation.VERTICAL, false, false, false);
		final NumberAxis domainAxis = (NumberAxis) chart.getXYPlot().getDomainAxis();
		domainAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		final XYLineAndShapeRenderer render = new XYLineAndShapeRenderer();
		render.setSeriesShapesVisible(0, true);
		chart.getXYPlot().setRenderer(render);
		ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
		
		c.gridy++;
		JPanel filler = new JPanel();
		filler.setPreferredSize(new Dimension(FILLERWIDTH, FILLERHEIGHT));
		filler.setBackground(Color.WHITE);
		center.add(filler, c);
		c.gridy++;
		center.add(chartPanel, c);
	}
}
